'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('wear_records', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '用户ID',
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      outfit_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '搭配ID',
        references: {
          model: 'outfits',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
      },
      wear_date: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '穿着日期',
      },
      occasion: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '场合',
      },
      location: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '地点',
      },
      weather: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '天气信息',
      },
      temperature: {
        type: Sequelize.DECIMAL(4, 1),
        allowNull: true,
        comment: '温度',
      },
      mood: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '心情',
      },
      rating: {
        type: Sequelize.DECIMAL(2, 1),
        allowNull: true,
        comment: '评分(1-5)',
      },
      photos: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '照片',
      },
      notes: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注',
      },
      tags: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '标签',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
    });

    // 添加索引
    await queryInterface.addIndex('wear_records', ['user_id']);
    await queryInterface.addIndex('wear_records', ['outfit_id']);
    await queryInterface.addIndex('wear_records', ['wear_date']);
    await queryInterface.addIndex('wear_records', ['occasion']);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('wear_records');
  },
};
